package wwiii;

import wwiii.exception.NotLoggedInException;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserManager {

	public boolean isLoggedIn() {
		final UserService us = UserServiceFactory.getUserService();

		return us.isUserLoggedIn();
	}

	public Player getPlayer() throws NotLoggedInException {
		final UserService us = UserServiceFactory.getUserService();
		final PlayerManager pm = new PlayerManager();

		if (!us.isUserLoggedIn()) {
			throw new NotLoggedInException();
		}
	
		final User user = us.getCurrentUser();
		final String id = user.getUserId();

		return pm.loadPlayer(id);
	}

	public void register(String name) throws NotLoggedInException {
		final UserService us = UserServiceFactory.getUserService();
		final PlayerManager pm = new PlayerManager();

		if (!us.isUserLoggedIn()) {
			throw new NotLoggedInException();
		}
	
		final User user = us.getCurrentUser();
		final String id = user.getUserId();
		final String mail = user.getEmail();

		pm.storePlayer(new Player(id, mail, name, 0));
	}
}
